
//生成模拟data数据
//引入mock
import { options } from "less";
import Mock, { Random } from "mockjs"
let List = []; //返回数据
// let id=1;
for (let i = 0; i < 100; i++) {
    List.push({
        id: i,
        name: Mock.Random.cname(),//随机生成一个名字
        age: Mock.Random.integer(18, 40),
        major: '黄金会员',
        address: Mock.Random.county(true),
        pay: Mock.Random.integer(0, 1),
        graduateTime: Mock.Random.date('yyyy-MM-dd')
    })
}
export default {
    //获取所有用户的数据
    userMock: (options) => {
        //请求体
        //搜索框值,每页显示,每页条数
        let { searchValue, page, size } = JSON.parse(options.body)
        // console.log(searchValue,page,size);
        //根据seach返回对应数据
        let newlist = List.filter((item) => {
            return item.name.indexOf(searchValue) != -1
        })
        // console.log(newlist);
        let newlist1 = newlist.slice((page - 1) * size, page * size)
        return {
            code: 200,
            msg: '请求成功',
            data: newlist1,
            totalpage: List.length,//返回总条数
            size: size,
            currentpage: page
        }
    },
    deleteMock: (options) => {
        //console.log(options)
        // 获取参数
        let id = options.url.split('?')[1].split('=')[1]
        console.log(id)
        let list1 = List.filter((item) => {
            return item.id != id
        })
        List = list1
        return {
            code: 200,
            msg: "删除成功",
            data: null
        }
    },
    editMock: (options) => {
        console.log(JSON.parse(options.body));
        let { name, age, address, graduateTime, id, major, pay } = JSON.parse(options.body)
        List.forEach(item => {
            if (item.id == id) {
                item.name = name,
                    item.age = age,
                    item.address = address,
                    item.graduateTime = graduateTime,
                    item.major = major,
                    item.pay = pay

            }
        })
        return {
            code: 200,
            msg: "修改成功",
            data: null
        }
    },
    //增加用户
    addMock: (options) => {
        let { address, age, graduateTime, major, name, pay } = JSON.parse(
            options.body
        );
        List.push({
            id: List.length,
            address,
            age,
            graduateTime,
            major,
            name,
            pay,
        });
        return {
            code: 200,
            msg: "添加成功",
            data: null,
        };
    },
}   